import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class TimeQuestionView implements IView
{

	private JDialog dialog;
	private QuizController itsQuizController;
	private TimeQuestion itsQuestion;
	private ClockPanel itsClockPanel;
	private JSlider hoursSlider, minutesSlider;
	
	public TimeQuestionView(TimeQuestion question, QuizController ctrl)
	{
		itsQuizController = ctrl;
		itsQuestion = question;
		itsClockPanel = new ClockPanel(new ClockGeometry());
	}
	
	@Override
	public void display(Window parent)
	{
		// dialog
		dialog = new JDialog(parent, itsQuestion.getTitle());
		dialog.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				itsQuizController.done();
			}
		});
		// buttons
		JButton prevBtn = new JButton("Previous");
		prevBtn.setPreferredSize(new Dimension(150, 80));
		FontManager.setComicFonts(prevBtn);
		if(itsQuizController.getCurrentQuestion() == 0)
		{
			prevBtn.setEnabled(false);
		}
        prevBtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				dialog.setVisible(false);
				itsQuizController.prev();
			}
		});
		
		JButton nextBtn = new JButton("Next");
		nextBtn.setPreferredSize(new Dimension(150, 80));
		FontManager.setComicFonts(nextBtn);
		nextBtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				dialog.setVisible(false);
				itsQuizController.next();
			}
		});
		// question text
		JLabel textLabel = new JLabel(itsQuestion.getText());
		textLabel.setForeground(new Color(0, 100, 0));
		textLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 30, 0));
		JLabel emptyLabel = new JLabel("");
		FontManager.setComicFonts(textLabel);
		// hour slider
		JLabel hoursLabel = new JLabel("Move this slider to set the hours");
		FontManager.setComicFonts(hoursLabel);
		hoursSlider = new JSlider(JSlider.HORIZONTAL, 1, 12, 1);
		FontManager.setComicFonts(hoursSlider);
		hoursSlider.setMajorTickSpacing(11);
		hoursSlider.setPaintTicks(true);
		hoursSlider.setPaintLabels(true);
		hoursSlider.setBorder(BorderFactory.createEmptyBorder(0, 0, 20, 0));
		hoursSlider.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e)
			{
				JSlider slider = (JSlider)e.getSource();
				int hour = slider.getValue();
				System.out.println("H: " + Integer.toString(hour));
				itsClockPanel.setHours(hour);
				int min = minutesSlider.getValue();
				String s = new String(Integer.toString(hour) + ":" + Integer.toString(min));
				itsQuizController.setCurrentResult(s);
			}
		});
		// minutes slider
		JLabel minutesLabel = new JLabel("Move this slider to set the minutes");
		FontManager.setComicFonts(minutesLabel);
		minutesSlider = new JSlider(JSlider.HORIZONTAL, 0, 55, 0);
		FontManager.setComicFonts(minutesSlider);
		minutesSlider.setMajorTickSpacing(5);
		minutesSlider.setSnapToTicks(true);
		minutesSlider.setPaintTicks(true);
		minutesSlider.setPaintLabels(true);
		minutesSlider.setBorder(BorderFactory.createEmptyBorder(0, 0, 20, 0));
		minutesSlider.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e)
			{
				JSlider slider = (JSlider)e.getSource();
				int minutes = slider.getValue();
				System.out.println("M: " + Integer.toString(minutes));
				itsClockPanel.setMinutes(minutes);
				int hour = hoursSlider.getValue();
				String s = new String(Integer.toString(hour) + ":" + Integer.toString(minutes));
				itsQuizController.setCurrentResult(s);
			}
		});
		// panel with time controls
		JPanel timeControlsPanel = new JPanel();
		GroupLayout grp = new GroupLayout(timeControlsPanel);
		timeControlsPanel.setLayout(grp);
		grp.setAutoCreateGaps(true);
		grp.setAutoCreateContainerGaps(true);
		
		GroupLayout.SequentialGroup hGroup = grp.createSequentialGroup();
		hGroup.addGroup(grp.createParallelGroup().addComponent(textLabel).addComponent(hoursLabel).addComponent(minutesLabel));
		hGroup.addGroup(grp.createParallelGroup().addComponent(emptyLabel).addComponent(hoursSlider).addComponent(minutesSlider));
		grp.setHorizontalGroup(hGroup);
		
		GroupLayout.SequentialGroup vGroup = grp.createSequentialGroup();
		vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).addComponent(textLabel).addComponent(emptyLabel));
		vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).addComponent(hoursLabel).addComponent(hoursSlider));
		vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).addComponent(minutesLabel).addComponent(minutesSlider));
		grp.setVerticalGroup(vGroup);
		// panel for everything except the buttons
		JPanel allPanel = new JPanel();
		allPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		allPanel.add(timeControlsPanel);
		allPanel.add(Box.createRigidArea(new Dimension(50, 0)));
		allPanel.add(itsClockPanel);
		allPanel.add(Box.createRigidArea(new Dimension(20, 0)));
		// buttons panel
		JPanel prevBtnPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		prevBtnPanel.add(prevBtn);
		JPanel nextBtnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		nextBtnPanel.add(nextBtn);
		JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
		btnPanel.add(prevBtnPanel);
		btnPanel.add(Box.createRigidArea(new Dimension(200, 0)));
		btnPanel.add(nextBtnPanel); 
		
		// add everything to our dialog
		dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.PAGE_AXIS));
		dialog.add(Box.createRigidArea(new Dimension(0, 20)));
		dialog.add(allPanel);
		dialog.add(Box.createRigidArea(new Dimension(0, 20)));
		dialog.add(btnPanel);
		
		itsClockPanel.setHours(1);
		itsClockPanel.setMinutes(0);
		if(itsQuizController.getCurrentResult().isEmpty())
		{
			itsQuizController.setCurrentResult(new String("1:00"));
		}
		
		dialog.pack();
		final Toolkit toolkit = Toolkit.getDefaultToolkit();
		final Dimension screenSize = toolkit.getScreenSize();
		final int x = (screenSize.width - dialog.getWidth()) / 2;
		final int y = (screenSize.height - dialog.getHeight()) / 2;
		dialog.setLocation(x, y);
		
		dialog.setVisible(true);
	}
	
	public void setTime(String time)
	{
		TimeConverter converter = new TimeConverter(time);
		itsClockPanel.setHours(converter.getHours());
		itsClockPanel.setMinutes(converter.getMinutes());
	}
}
